package LearnDataStructure.a_线性结构.列表.顺序表;

import LearnDataStructure.a_线性结构.列表.MyList;

import java.util.Arrays;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-07-01 15:31
 * @description: LeetcodeLearnAlgorithm -> LearnDataStructure.a_线性结构.列表.顺序表
 */
public class MyListLinear<T> implements MyList<T> {
    private T[] elements;//真正存储元素的底层结构
    private int size = 0;//结构真实存放元素个数
    private int capacity = 10;//结构最大存放元素个数
    private int now = 0;

    public MyListLinear(int capacity) {
        this.capacity = capacity;//用户自定义最大数量
        elements = (T[]) new Object[capacity];//在构造器中，马上生成数组
    }

    public MyListLinear() {
        elements = (T[]) new Object[capacity];
    }

    @Override
    public void add(T element) {
        if (size == capacity) {//扩容
            capacity *= 2;
            T[] newElements = (T[]) new Object[capacity];
            for (int i = 0; i < size; i++) {//size不变
                newElements[i] = elements[i];
            }
            elements = newElements;//地址覆盖
        }
        elements[size++] = element;
    }

    @Override
    public void delete(T element) {//委托转化，很巧妙
        int TargetIndex = indexOf(element);
        if (TargetIndex >= 0) {
            delete(TargetIndex);
        }
    }

    @Override
    public void delete(int index) {
        for (int i = index; i < size - 1; i++) {
            elements[i] = elements[i + 1];
        }
        elements[size - 1] = null;
        size--;
    }

    @Override
    public void update(int index, T newElement) {
        if (index < 0 || index >= size) {
            System.out.println("ERROR: index < 0 || index > size; index = " + index + "size = " + size);
        } else {
            elements[index] = newElement;
        }
    }

    @Override
    public boolean contains(T target) {
        return indexOf(target) >= 0;//很巧妙
    }

    @Override
    public T AtElement(int index) {
        if (index < 0 || index >= size) {
            System.out.println("ERROR: index < 0 || index > size; index = " + index + "size = " + size);
        } else {
            return elements[index];
        }
        return null;
    }

    @Override
    public int indexOf(T element) {
        if (element != null) {
            for (int i = 0; i < size; i++) {
                if (elements[i].equals(element)) {
                    return i;
                }
            }
        }
        return -1;
    }

    public void Print() {
        System.out.println(
                "MyListLinear{" +
                        "elements=" + Arrays.toString(elements) +
                        ", size=" + size +
                        ", capacity=" + capacity +
                        '}'
        );
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("[");
        for (int i = 0; i < size; i++) {
            stringBuilder.append(elements[i] + (i == size - 1 ? "" : " , "));
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    @Override
    public boolean hasNext() {
        return now != size;
    }

    @Override
    public T next() {
        return elements[now++];
    }
}
